/**
 * 
 */
package com.meetingshow.privilege.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.SaveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.privilege.dao.model.UserPO;
import com.meetingshow.privilege.service.IUserService;
import com.meetingshow.privilege.service.model.UserQueryParas;
import com.meetingshow.privilege.uiservice.model.UserQueryParasVO;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.utils.BaseUtil;

/**
 * @author louyi
 *
 */
public class UIUserServiceImp extends BaseUIServiceImp implements IUIUserService {
    
    private final static Logger logger = Logger.getLogger(UIUserServiceImp.class);

    private IUserService userService;
    
    /**
     * @return the userService
     */
    public IUserService getUserService() {
        return userService;
    }

    /**
     * @param userService the userService to set
     */
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Override
    public Integer saveUser(UserVO userVO) throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userVO)) {
            this.throwValidaterException("userVO is null.");
        }
        
        userVO.setCreateTime(new Date());
        
        validateThrowEx(userVO, SaveValidator.class);
        
        UserQueryParas userQueryParas = new UserQueryParas();
        userQueryParas.setUserName_e(userVO.getUserName());
        
        QueryResults<UserPO> userQueryResults = null;
        try {
            userQueryResults = getUserService().queryUser(userQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(userQueryResults)) {
            logger.error("userName [ " + userVO.getUserName() + " ] exists.");
            throw new BaseUIServiceException("userName [ " + userVO.getUserName() + " ] exists.");
        }
        
        userQueryParas = new UserQueryParas();
        userQueryParas.setEmail_e(userVO.getEmail());
        userQueryResults = null;
        try {
            userQueryResults = getUserService().queryUser(userQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(userQueryResults)) {
            logger.error("email [ " + userVO.getEmail() + " ] exists.");
            throw new BaseUIServiceException("email [ " + userVO.getEmail() + " ] exists.");
        }
        
        
        try {
            Integer userId = getUserService().saveUser(userVO.toUserPO());
            userVO.setUserId(userId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return userVO.getUserId();
    }

    @Override
    public void updateUser(UserVO userVO) throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userVO)) {
            this.throwValidaterException("userVO is null.");
        }
        
        validateThrowEx(userVO, UpdateValidator.class);
        
        UserQueryParas userQueryParas = new UserQueryParas();
        userQueryParas.setUserId_e(String.valueOf(userVO.getUserId()));
        
        QueryResults<UserPO> userQueryResults = null;
        try {
            userQueryResults = getUserService().queryUser(userQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(userQueryResults)) {
            UserPO persistUserPO = userQueryResults.getResultList().get(0);
            persistUserPO.setEmail(userVO.getEmail());
            persistUserPO.setPassword(userVO.getPassword());
            persistUserPO.setUserType(userVO.getUserType());
            
            try {
                getUserService().updateUser(persistUserPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public void removeUser(UserVO userVO) throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userVO)) {
            this.throwValidaterException("userVO is null.");
        }
        
        validateThrowEx(userVO, RemoveValidator.class);
        
        UserQueryParas userQueryParas = new UserQueryParas();
        userQueryParas.setUserId_e(String.valueOf(userVO.getUserId()));
        
        QueryResults<UserPO> userQueryResults = null;
        try {
            userQueryResults = getUserService().queryUser(userQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(userQueryResults)) {
            UserPO persistUserPO = userQueryResults.getResultList().get(0);
            try {
                getUserService().removeUser(persistUserPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public UserVO loadUserById(Integer userId) throws BaseUIServiceException {
        if (userId == null)
            this.throwValidaterException("userId is null.");
        
        UserPO userPO = null;
        try {
            userPO = getUserService().loadUserById(userId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (userPO == null)
            return null;
        else
            return new UserVO(userPO);
    }
    
    @Override
    public QueryResults<UserVO> queryUser(UserQueryParasVO userQueryParasVO) throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userQueryParasVO)) {
            this.throwValidaterException("userQueryParasVO is null.");
        }
        
        validateThrowEx(userQueryParasVO);
        
        QueryResults<UserPO> userPOQueryResults = null;
        try {
            userPOQueryResults = getUserService().queryUser(userQueryParasVO.toUserQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        QueryResults<UserVO> userVOQueryResults = new QueryResults<UserVO>();
        List<UserVO> userVOList = new ArrayList<UserVO>();
        if (QueryResults.resultsListNotNull(userPOQueryResults)) {
            for (UserPO userPO : userPOQueryResults.getResultList()) {
                if (BaseUtil.objectNotNull(userPO)) {
                    userVOList.add(new UserVO(userPO));
                }
            }
        }
        
        userVOQueryResults.setFirstResultNo(userPOQueryResults.getResultsPerGroup());
        userVOQueryResults.setResultsPerGroup(userPOQueryResults.getResultsPerGroup());
        userVOQueryResults.setResultList(userVOList);
        userVOQueryResults.setTotalResultsNum(userPOQueryResults.getTotalResultsNum());
        
        return userVOQueryResults;
    }

}
